package com.actions.bluetoothbox2.util;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.actions.bluetoothbox2.R;
import com.actions.ibluz.manager.BluzManager;

import java.util.List;

/**
 * Created by liuyong on 2018/6/11.
 */

public class TimeTableAdapter extends BaseAdapter {

    private Context context;
    private BluzManager mBluzManager;
    private int[] actions = new int[]{R.drawable.ic_action_1, R.drawable.ic_action_2, R.drawable.ic_action_7,
            R.drawable.ic_action_3, R.drawable.ic_action_4, R.drawable.ic_action_8, R.drawable.ic_action_6,
            R.drawable.ic_action_5, R.drawable.ic_action_9};
    private int voices[] = new int[]{R.drawable.ic_voice_1, R.drawable.ic_voice_2, R.drawable.ic_voice_3, R.drawable.ic_voice_4,
            R.drawable.ic_voice_5, R.drawable.ic_voice_6, R.drawable.ic_voice_7, R.drawable.ic_voice_8, R.drawable.ic_voice_9,
            R.drawable.ic_voice_10, R.drawable.ic_voice_11, R.drawable.ic_voice_12, R.drawable.ic_voice_13, R.drawable.ic_voice_14,
            R.drawable.ic_voice_15};
    private int led_imgID[] = new int[]{R.drawable.ic_color1, R.drawable.ic_color2, R.drawable.ic_color3, R.drawable.ic_color4,
            R.drawable.ic_color5, R.drawable.ic_color6, R.drawable.ic_color7, R.drawable.ic_color8, R.drawable.ic_color9,
            R.drawable.ic_color10, R.drawable.ic_color11, R.drawable.ic_color12, R.drawable.ic_color13, R.drawable.ic_color14,
            R.drawable.ic_color15};

    List<ActionAlarmInfo> list;

    public TimeTableAdapter(Context context, List<ActionAlarmInfo> list,BluzManager mBluzManager) {
        this.context = context;
        this.list = list;
        this.mBluzManager = mBluzManager;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (null == view) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.item_time_table, viewGroup, false);
            viewHolder.tv_action = (TextView) view.findViewById(R.id.tv_action);
            viewHolder.tv_start_time = (TextView) view.findViewById(R.id.tv_start_time);
            viewHolder.tv_stop_time = (TextView) view.findViewById(R.id.tv_stop_time);
            viewHolder.tv_voice = (TextView) view.findViewById(R.id.tv_voice);

            viewHolder.img_action = (ImageView) view.findViewById(R.id.img_action);
            viewHolder.img_voice = (ImageView) view.findViewById(R.id.img_voice);
            viewHolder.img_color = (ImageView) view.findViewById(R.id.img_color);
            viewHolder.img_color_view = view.findViewById(R.id.img_color_view);
            viewHolder.checkbox_onoff = (Switch) view.findViewById(R.id.checkbox_onoff);
            view.setTag(viewHolder);
        } else viewHolder = (ViewHolder) view.getTag();

        if (list.get(i).alarm_id < 9) {
//            viewHolder.img_action.setImageResource(0);
            viewHolder.img_action.setImageResource(actions[list.get(i).alarm_id - 1]);
        } else {
            viewHolder.img_action.setImageResource(actions[8]);
        }
        if (list.get(i).alarm_type == 3) {
            viewHolder.img_voice.setImageResource(voices[list.get(i).ring_seq - 1]);
        } else {
            viewHolder.img_voice.setImageResource(voices[14]);
        }
        viewHolder.tv_action.setText(list.get(i).name);
        viewHolder.tv_start_time.setText(list.get(i).alarm_hour + ":" + list.get(i).alarm_m);

        viewHolder.tv_stop_time.setText(list.get(i).ring_endtime + "");

        if (list.get(i).led_action == 0) {
            if(list.get(i).led_R == 0&&list.get(i).led_G==0&&list.get(i).led_B==0){
                viewHolder.img_color.setVisibility(View.GONE);
                viewHolder.img_color_view.setVisibility(View.GONE);
            }else {
                viewHolder.img_color.setVisibility(View.GONE);
                viewHolder.img_color_view.setVisibility(View.VISIBLE);
                GradientDrawable myGrad = (GradientDrawable) viewHolder.img_color_view.getBackground();
                myGrad.setColor(Color.rgb(list.get(i).led_R, list.get(i).led_G, list.get(i).led_B));
            }
        } else {
            switch (list.get(i).led_action) {
                case 0x80:
                    viewHolder.img_color.setImageResource(led_imgID[0]);
                    break;
                case 0x81:
                    viewHolder.img_color.setImageResource(led_imgID[1]);
                    break;
                case 0x82:
                    viewHolder.img_color.setImageResource(led_imgID[2]);
                    break;
                case 0x83:
                    viewHolder.img_color.setImageResource(led_imgID[3]);
                    break;
                case 0x84:
                    viewHolder.img_color.setImageResource(led_imgID[4]);
                    break;
                case 0x85:
                    viewHolder.img_color.setImageResource(led_imgID[5]);
                    break;
                case 0x86:
                    viewHolder.img_color.setImageResource(led_imgID[6]);
                    break;
                case 0x87:
                    viewHolder.img_color.setImageResource(led_imgID[7]);
                    break;
                case 0x88:
                    viewHolder.img_color.setImageResource(led_imgID[8]);
                    break;
                case 0x89:
                    viewHolder.img_color.setImageResource(led_imgID[9]);
                    break;
                case 1:
                    viewHolder.img_color.setImageResource(led_imgID[10]);
                    break;
                case 2:
                    viewHolder.img_color.setImageResource(led_imgID[11]);
                    break;
                case 3:
                    viewHolder.img_color.setImageResource(led_imgID[12]);
                    break;
                case 4:
                    viewHolder.img_color.setImageResource(led_imgID[13]);
                    break;
                default:
                    break;
            }
            viewHolder.img_color.setVisibility(View.VISIBLE);
            viewHolder.img_color_view.setVisibility(View.GONE);
        }

        if (list.get(i).alarm_onoff == 1) viewHolder.checkbox_onoff.setChecked(true);
        else viewHolder.checkbox_onoff.setChecked(false);

        viewHolder.checkbox_onoff.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

            }
        });
        return view;
    }

    public class ViewHolder {
        TextView tv_action, tv_start_time, tv_stop_time, tv_voice;
        ImageView img_action, img_voice, img_color;
        View img_color_view;
        Switch checkbox_onoff;
    }
    public void SendData(ActionAlarmInfo info,byte isopen) {

        byte[] bytes = new byte[84];
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = 0;
        }

        bytes[0] = (byte) info.alarm_id;
        bytes[1] = 1;
        bytes[2] = isopen;
        bytes[3] = (byte) info.alarm_day;

        int h = info.alarm_hour;
        bytes[4] = (byte) h;
        int m = info.alarm_m;
        bytes[5] = (byte) m;
        bytes[6] = 0;
        bytes[7] = (byte) info.alarm_type;

        byte[] bytes1 = Utils.intToBytes(info.ring_seq);
        bytes[12] = bytes1[0];
        bytes[13] = bytes1[1];

        bytes[8]= (byte) info.led_action;
        bytes[9] = (byte) info.led_R;
        bytes[10] = (byte) info.led_G;
        bytes[11] = (byte) info.led_B;

        int endTime = info.ring_endtime;
        byte[] bytes2 = Utils.intToBytes(endTime);
        bytes[14] = bytes2[0];
        bytes[15] = bytes2[1];

        int volume =info.led_volume;
        bytes[16] = (byte) volume;
        bytes[17] = 0;
        bytes[18] = 0;
        bytes[19] = 0;

        byte[] b_title =info.name.getBytes();
        if (b_title.length < 32) {
            for (int i = 0; i < b_title.length; i++) {
                bytes[i + 20] = b_title[i];
            }
        } else {
            for (int i = 0; i < 32; i++) {
                bytes[i + 20] = b_title[i];
            }
        }
        int cmd_type = Integer.decode("0x53");
        int cmd_id = Integer.decode("0x9b");
        mBluzManager.sendCustomCommand(cmd_type << 8 | cmd_id, 0, 0, bytes);
    }

}
